﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using libInventory.BUS;
using Telerik.WinControls.UI;
using libCommonTools;
using libInventory.BUS;
using libDAL;
namespace libInventory.FORMS
{
    public partial class frmFindCharges : Telerik.WinControls.UI.RadForm
    {
        private clsBUSFindCharges _BUS;
        private clsTimerDelay TimerDelay;
        private int intXCoordinate = 0;
        private int intYCoordinate = 0;
        private string strSearchType = string.Empty;
        private string strCode = string.Empty;
        private string strDescription = string.Empty;
        private string strSalesTermSetupHeaderID = string.Empty;
        private string strSalesTermSetupDetailsID = string.Empty;
        private DataRow[] drDetailsCharges;
        public string getset_Code
        {
            get
            {
                return strCode;
            }
            set
            {
                strCode = value;
            }
        }

        public string getset_SetupHeaderID
        {
            get
            {
                return strSalesTermSetupHeaderID;
            }
            set
            {
                strSalesTermSetupHeaderID = value;
            }
        }

        public string getset_SetupDetailsID
        {
            get
            {
                return strSalesTermSetupDetailsID;
            }
            set
            {
                strSalesTermSetupDetailsID = value;
            }
        }

        public string getset_Description
        {
            get
            {
                return strDescription;
            }
            set
            {
                strDescription = value;
            }
        }

        public string getset_SearchType
        {
            get
            {
                return strSearchType;
            }
            set
            {
                strSearchType = value;
            }
        }

        public string getset_SalesTermSetupHeaderID
        {
            get
            {
                return strSalesTermSetupHeaderID;
            }
            set
            {
                this.strSalesTermSetupHeaderID = value;
            }
        }

        public frmFindCharges()
        {
            _BUS = new clsBUSFindCharges();
            InitializeComponent();
        }

        public cls_DAL set_DAL
        {
            set
            {
                _BUS._DAL = value;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (TimerDelay.get_CheckStoped == true)
            {
                set_LoadToGrid(_BUS.get_SearchResult(txtSearch.Text.Trim(), rboCode.IsChecked));
                TimerDelay.get_CheckStoped = false;
            }
        }

        private void set_LoadToGrid(DataTable dtSource)
        {
            try
            {
                if (getset_SalesTermSetupHeaderID == string.Empty)
                {
                    dgvCharges.DataSource = dtSource;
                    ((GridTableElement)this.dgvCharges.TableElement).RowHeight = 20;
                    dgvCharges.BestFitColumns();
                }
                else
                {
                    dgvCharges.DataSource = dtSource;
                    ((GridTableElement)this.dgvCharges.TableElement).RowHeight = 20;
                    dgvCharges.BestFitColumns();
                    dgvCharges.Columns[0].IsVisible = false;
                    dgvCharges.Columns[1].IsVisible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_LoadToGrid"); 
            }
        }

        private void frmFindCharges_Load(object sender, EventArgs e)
        {
            try
            {
                timer1.Start();
                TimerDelay = new clsTimerDelay();
                _BUS.getset_SalesTermSetupHeaderID = this.getset_SalesTermSetupHeaderID;
                set_LoadToGrid(_BUS.get_Charges());
                this.Location = new Point(intXCoordinate, intYCoordinate);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " frmFindCharges_Load"); 
            }
        }

        public void set_NewLocation(int intX, int intY)
        {
            intXCoordinate = intX;
            intYCoordinate = intY;
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtSearch.Text.Length < 2 && txtSearch.Text.Length > 0)
                {
                    TimerDelay.RevokeQueryTimer();
                }
                else if (txtSearch.Text.Length == 0)
                {
                    TimerDelay.RevokeQueryTimer();
                    set_LoadToGrid(_BUS.get_Charges());
                }
                else
                {
                    TimerDelay.RestartQueryTimer();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " txtSearch_TextChanged"); 
            }
        }

        public DataTable get_ChargeDetails()
        {
                return _BUS.get_DetailsSetupDetails(getset_SetupDetailsID);
        }

        private void set_CodeDesc()
        {
            try
            {
                if (dgvCharges.SelectedRows.Count > 0)
                {
                    if (this.getset_SalesTermSetupHeaderID == string.Empty)
                    {
                        getset_Description = dgvCharges.CurrentRow.Cells[1].Value.ToString();
                        getset_Code = dgvCharges.CurrentRow.Cells[0].Value.ToString();
                    }
                    else
                    {
                        //[Item Code(s)], mfchaitemmain.ccitemdes as [Description(s)] 
                        getset_SetupDetailsID = dgvCharges.CurrentRow.Cells["SalesTermSetupAdditionalChargesID"].Value.ToString();
                        getset_Description = dgvCharges.CurrentRow.Cells["Description(s)"].Value.ToString();
                        getset_Code = dgvCharges.CurrentRow.Cells["Item Code(s)"].Value.ToString();
                        getset_SetupHeaderID = dgvCharges.CurrentRow.Cells["SalesTermSetupHeadersID"].Value.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_CodeDesc"); 
            }
        }

        private void dgvCharges_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            set_CodeDesc();
            DialogResult = DialogResult.OK; ;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            set_CodeDesc();
            DialogResult = DialogResult.OK;
        }
    }
}
